package com.itheima.controller;


import com.itheima.common.R;
import com.itheima.domain.Menu;
import com.itheima.domain.Resource;
import com.itheima.domain.Role;
import com.itheima.manager.UmsRoleManager;
import com.itheima.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UmsRoleController {
    @Autowired
    UmsRoleManager umsRoleManager;

    //获取所有已启用角色
    @GetMapping("/role/listAll")
    public R<List<Role>> findAll(){
        return umsRoleManager.findAll();
    }

    //根据角色名称分页获取角色
    @GetMapping("/role/list")
    public R<PageBeanVo> findByConditionAndPage(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                                @RequestParam(value="pageSize",defaultValue = "5")Integer pageSize,
                                                @RequestParam(value="keyword",required = false) String keyword){
        return umsRoleManager.findByConditionAndPage(pageNum,pageSize,keyword);
    }

    //添加角色
    @PostMapping("/role/create")
    public R<String> saveRole(@RequestBody Role role){
        return umsRoleManager.saveRole(role);
    }

    //给角色分配菜单
    @PostMapping("/role/allocMenu")
    public R<String> updateRoleMenu(String roleId,String menuIds){
        return umsRoleManager.updateRoleMenu(roleId,menuIds);
    }

    //给角色分配资源
    @PostMapping("/role/allocResource")
    public R<String> updateRoleResource(String roleId,String resourceIds){
        return umsRoleManager.updateRoleResource(roleId,resourceIds);
    }

    //批量删除角色
    @PostMapping("/role/delete")
    public R<String> deleteByIds(String ids){
        return umsRoleManager.deleteByIds(ids);
    }

    //获取角色相关菜单
    @GetMapping("/role/listMenu/{roleId}")
    public R<List<Menu>> findMenuByRoleId(@PathVariable Long roleId){
        return umsRoleManager.findMenuByRoleId(roleId);
    }

    //获取角色相关资源
    @GetMapping("/role/listResource/{roleId}")
    public R<List<Resource>> findResourceByRoleId(@PathVariable Long roleId){
        return umsRoleManager.findResourceByRoleId(roleId);
    }

    //修改角色
    @PostMapping("/role/update/{id}")
    public R<String> updateById(@PathVariable Long id,@RequestBody Role role){
        return umsRoleManager.updateById(role);
    }

    //根据id修改角色状态
    @PostMapping("/role/updateStatus/{id}")
    public R<String> updateStatus(@PathVariable Long id,Integer status){
        return umsRoleManager.updateStatus(id,status);
    }
}
